package com.beiding.listener;

import com.beiding.dao.OrderDao;
import com.beiding.dao.SettingsDao;
import com.beiding.pojo.order.Evaluation;
import com.beiding.pojo.order.Order;
import com.beiding.service.OrderService;
import com.beiding.service.impl.MessageSender;
import com.beiding.timeout.bean.TimeShaft;
import com.beiding.utils.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Evaluate extends TimeoutListener {

    private OrderService orderService;

    private MessageSender sender;


    private SettingsDao settings;

    @Autowired
    public void setSettings(SettingsDao settings) {
        this.settings = settings;
    }

    @Autowired
    public void setSender(MessageSender sender) {
        this.sender = sender;
    }

    @Autowired
    public void setOrderService(OrderService orderService) {
        this.orderService = orderService;
    }

    protected Evaluate(OrderDao orderDao, TimeShaft<String> timeShaft) {
        super(orderDao, timeShaft, "evaluate");
    }

    @Override
    public void run(String id) {
        Order order = orderDao.getInsensitive(id);

        if (judge(order, Order.EVALUATE)) {

            LogUtils.info(this.getClass(),"超时未评论默认好评");

            Evaluation evaluate = new Evaluation();
            evaluate.setEvaluation("默认好评");
            evaluate.setService(5);
            evaluate.setEnvironment(5);
            try {
                orderService.evaluate(id, evaluate);
            } catch (Exception ignore) {
            }



            orderDao.updateField(id, "state", Order.TERMINATION);

            order.setState(Order.TERMINATION);

            sender.sendToTenant(order.getHolderId(),update(order));

            timeShaft.delay("delete", settings.getTimeout("delete"), id);

        }
    }
}

